package com.yunhe.sms.controller.activity;

import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.util.TimeUtils;
import com.yunhe.sms.dao.activity.VoteLogDao;
import com.yunhe.sms.dao.activity.impl.VoteLogDaoImpl;
import com.yunhe.sms.entity.activity.Vote;
import com.yunhe.sms.entity.activity.VoteItem;
import com.yunhe.sms.entity.activity.VoteLog;
import com.yunhe.sms.service.activity.VoteItemService;
import com.yunhe.sms.service.activity.VoteService;
import com.yunhe.sms.service.activity.impl.VoteItemServiceImpl;
import com.yunhe.sms.service.activity.impl.VoteServiceImpl;
import com.yunhe.webmvc.annotation.RequestBean;
import com.yunhe.webmvc.annotation.RequestMapping;
import com.yunhe.webmvc.annotation.ResponseJson;
import com.yunhe.webmvc.model.JSONResponse;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/10/30 0030.
 */
@RequestMapping("/voteitem")
public class VoteItemController {

    private VoteItemService voteItemService = new VoteItemServiceImpl();
    private VoteService voteService = new VoteServiceImpl();
    private VoteLogDao voteLogDao = new VoteLogDaoImpl();

    @RequestMapping
    public String list(){
        return "activity/voteitem/list";
    }

    @RequestMapping("/add")
    public String add(String voteId,HttpServletRequest req){
        /*req.setAttribute("voteId",voteId);*/
        return "activity/voteitem/add";
    }

    @RequestMapping("/edit")
    public String edit(Long id, HttpServletRequest req){
        VoteItem voteItem = voteItemService.find(id);
        req.setAttribute("voteItem",voteItem);
        return "activity/voteitem/edit";
    }

    @RequestMapping("/save")
    @ResponseJson
    public JSONResponse save(@RequestBean VoteItem voteItem){
        voteItemService.save(voteItem);
        return JSONResponse.success("添加成功");

    }

    @RequestMapping("/delete")
    @ResponseJson
    public JSONResponse delete(String ids){
        voteItemService.deleteBatch(ids);

        return JSONResponse.success("删除成功");
    }

    @RequestMapping("/update")
    @ResponseJson
    public JSONResponse update(@RequestBean VoteItem voteItem){
        voteItemService.update(voteItem);
        return JSONResponse.success("修改成功");

    }
    @RequestMapping("/listData")
    @ResponseJson
    public Pageable<VoteItem> listDate(Integer offset, Integer limit, Long voteId){
        return voteItemService.findPage(offset,limit,voteId);
    }

    @RequestMapping("/toupiao")
    public String toupiao(Long voteId, HttpServletRequest req){
        if(null == voteId){
            voteId = 1L;
        }
        Vote vote = voteService.find(voteId);

       /* Boolean flag = voteItemService.toupiao(voteId, req);
         req.setAttribute("flag",flag);
*/

       String flag = "";
        Cookie[] cookies = req.getCookies();

        Long nowTime = new Date().getTime();

        Long startTime = vote.getStartTime().getTime();
        Long endTime = vote.getEndTime().getTime();

        if(nowTime<startTime){
            flag = "true";
        }else if(nowTime >endTime){
            flag = "true";
        }else if(null != cookies) {
            for (Cookie c : cookies) {
                if (c.getName().equals(voteId.toString()) && c.getValue().equals(voteId.toString())) {
                    flag = "true";
                    break;
                }
            }
        }



        req.setAttribute("flag",flag);


        if(vote.getStatus()==0) {
            req.setAttribute("status",0);
            /*throw new RuntimeException();*/
        }else{
            List<VoteItem> voteItems = voteItemService.findList(voteId);
            req.setAttribute("vote",vote);
            req.setAttribute("voteItems",voteItems);

        }
        return "activity/voteitem/toupiao";
    }


    @RequestMapping("/updateVotes")
    @ResponseJson
    public JSONResponse updateVotes(Long itemId, Long voteId, HttpServletRequest req, HttpServletResponse resp){

        Boolean flag = true;
        String message = "投票成功";
        int code = 1;
        Cookie[] cookies = req.getCookies();

        Vote vote = voteService.find(voteId);

        Long nowTime = new Date().getTime();

        Long startTime = vote.getStartTime().getTime();
        Long endTime = vote.getEndTime().getTime();

        if(nowTime<startTime){
            message = "投票未开始";
            code = 0;
            flag=false;
        }else if(nowTime >endTime){
            message = "投票已结束";
            code = 0;
            flag=false;
        }else if(null != cookies){
            for(Cookie c:cookies){
                if(c.getName().equals(voteId.toString())&&c.getValue().equals(voteId.toString())){
                    message = "每天只能投一票";
                    code = 0;
                    flag=false;
                    break;
                }
            }
        }

        if(flag){
            voteItemService.updateVotes(itemId);
            VoteItem voteItem = voteItemService.find(itemId);
            Cookie cookie = new Cookie(voteId.toString(),voteId.toString());
            cookie.setMaxAge(3600*24);
            cookie.setPath("/");
            resp.addCookie(cookie);
        }
        return new JSONResponse(code,message);

       /* voteItemService.updateVotes(itemId);
        VoteItem voteItem = voteItemService.find(itemId);

        VoteLog voteLog = voteItemService.mac(voteId, req);
        voteLogDao.save(voteLog);

        return JSONResponse.success("投票成功",voteItem.getVotes());*/
    }





}
